﻿using CommonTool;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Tool;

namespace Motions
{
    class LTDMotion : Motions
    {
        public LTDMotion(int nCardIndex, string strName, int nMinAxisNo, int nMaxAxisNo)
            : base(nCardIndex, strName, nMinAxisNo, nMaxAxisNo)
        {
            m_bEnable = false;
        }

        /// <summary>
        /// 板卡初始化
        /// </summary>
        /// <returns></returns>
        public override void InitMotionCard()
        {
            int count = 0;
            short rtn = 0;
            count = LTDMC.dmc_board_init();//板卡初始化
            if (count == 0)
            {
                //MessageBox.Show(string.Format("没有找到雷赛运动控制卡"));
               LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("没有找到雷赛运动控制卡"));
                return;
            }
            rtn += LTDMC.dmc_download_configfile((ushort)m_nCardIndex, AppDomain.CurrentDomain.BaseDirectory + "AxisPara.ini");//下载轴参数文件
            if (0 == rtn)
            {
                m_bEnable = true;//初始化成功   
                return;       
            }
            m_bEnable = false;
            MessageBox.Show(string.Format("雷赛运动控制卡初始化打开失败{0}{1}", m_nCardIndex,rtn));
            LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛运动控制卡初始化打开失败{0}{1}", m_nCardIndex, rtn));      
        }

        /// <summary>
        /// 关闭轴卡
        /// </summary>
        /// <returns></returns>
        public override void DeInitMotionCard()
        {
            short rtn = 0;
            rtn += LTDMC.dmc_board_close();
            if (rtn != 0)
            {
                MessageBox.Show(string.Format("雷赛运动控制卡关闭失败{0}{1}", m_nCardIndex, rtn));
                LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛运动控制卡关闭失败{0}{1}", m_nCardIndex, rtn));
            }
        }

        /// <summary>
        /// 开启使能
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void ServoOn(int nAxisNo)
        {
            short rtn = 0;
            rtn += LTDMC.nmc_set_axis_enable((ushort)m_nCardIndex, (ushort)nAxisNo);
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡开启使能失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex,nAxisNo,rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡开启使能失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 关闭使能
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void ServoOff(int nAxisNo)
        {
            short rtn = 0;
            rtn += LTDMC.nmc_set_axis_disable((ushort)m_nCardIndex, (ushort)nAxisNo);//关闭轴卡 
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡关闭使能失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡关闭使能失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 清除轴报警信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>                   
        public override void ClearAxisAlarm(int nAxisNo)
        {
            short rtn = 0;
            rtn = LTDMC.nmc_clear_axis_errcode((ushort)m_nCardIndex, (ushort)nAxisNo);//关闭轴卡 
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡清除报警失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡清除报警失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 读取指定轴Busy状态
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisBusy(int nAxisNo)
        {
            short rtn = 0;
            rtn = LTDMC.dmc_check_done((ushort)m_nCardIndex, (ushort)nAxisNo);//读取轴运动状态 0 运动中 1停止中
            if (rtn == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 读取指定轴报警信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisAlarm(int nAxisNo)
        {
            //0 ALM 伺服报警 1 ON 0 OFF 0x1
            //1 EL+ 正硬限位            0x2
            //2 EL- 负硬限位            0x4
            //3 EMG 急停信号            0x8
            //4 ORG 原点信号            0x10    
            //6 SL+ 正软限位            0x40
            //7 SL- 负软限位            0x80
         
            uint sts = 0;//轴状态字
            sts = LTDMC.dmc_axis_io_status((ushort)m_nCardIndex, (ushort)nAxisNo);//读取指定轴有关信号的状态
            if ( (sts & 0x1) != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡读取报警失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡读取报警失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                return true;
            }
            return false;
           

        }

        /// <summary>
        /// 读取指定轴伺服准备信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisReady(int nAxisNo)
        {
            // 0  未启动状态
            // 1 启动禁止状态
            //2 准备启动状态
            //3 启动状态
            //4 操作使能状态
            //5 停止状态
            //6 错误触发状态
            //7 错误状态
            short rtn = 0;
            ushort sts = 0;//轴状态字
            rtn  = LTDMC.nmc_get_axis_state_machine((ushort)m_nCardIndex, (ushort)nAxisNo,ref sts);//读取指定轴有关信号的状态
            if (rtn == 0)
            {
                if (sts ==  3)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 读取轴使能状态
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisSeverOn(int nAxisNo)
        {
            // 0  未启动状态
            // 1 启动禁止状态
            //2 准备启动状态
            //3 启动状态
            //4 操作使能状态
            //5 停止状态
            //6 错误触发状态
            //7 错误状态
            short rtn = 0;
            ushort sts = 0;//轴状态字
            rtn = LTDMC.nmc_get_axis_state_machine((ushort)m_nCardIndex, (ushort)nAxisNo , ref sts);//读取使能状态 
            if (rtn == 0)
            {
                if (sts == 4)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取指定轴当前位置
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override double GetAxisActPos(int nAxisNo)
        {
            short rtn = 0;
            double pos = 0;
            rtn = LTDMC.dmc_get_encoder_unit((ushort)m_nCardIndex, (ushort)nAxisNo, ref pos); //读取当前编码器计数值
            if (rtn == 0)
            {
                return pos / SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            }
            return 0;
        }

        /// <summary>
        /// 获取指定轴命令位置
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override double GetAxisPrfPos(int nAxisNo)
        {
            short rtn = 0;
            double pos = 0;          
            rtn = LTDMC.dmc_get_position_unit((ushort)m_nCardIndex, (ushort)nAxisNo, ref pos); //读取当前指令位置计数值
            if (rtn == 0)
            {
                return pos / SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            }
            return 0;
        }

        /// <summary>
        /// 获取轴定位完成信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisDone(int nAxisNo)
        {
            //0 ALM 伺服报警 1 ON 0 OFF 0x1
            //1 EL+ 正硬限位            0x2
            //2 EL- 负硬限位            0x4
            //3 EMG 急停信号            0x8
            //4 ORG 原点信号            0x10    
            //6 SL+ 正软限位            0x40
            //7 SL- 负软限位            0x80


            // 0  未启动状态
            // 1 启动禁止状态
            //2 准备启动状态
            //3 启动状态
            //4 操作使能状态
            //5 停止状态
            //6 错误触发状态
            //7 错误状态

            short rtn = 0;
            short rtn1 = 0;
            ushort sts = 0;//轴状态字
            uint sts1 = 0;//轴状态字
            rtn += LTDMC.nmc_get_axis_state_machine((ushort)m_nCardIndex, (ushort)nAxisNo, ref sts);//读取使能状态 
            rtn1 = LTDMC.dmc_check_done((ushort)m_nCardIndex, (ushort)nAxisNo);//读取轴运动状态 0 运动中 1停止中
            sts1 = LTDMC.dmc_axis_io_status((ushort)m_nCardIndex, (ushort)nAxisNo);//读取指定轴有关信号的状态
            if (rtn == 0)
            {
                if (4 == sts )//有使能
                {
                    if (0 == (sts1 & 0x4))//负极限未触发
                    {
                        if (0 == (sts1 & 0x2))//正极限未触发
                        {
                            if (0 == (sts1 & 0x1))//无伺服报警
                            {
                                if (1 == rtn1 )//轴停止中
                                {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return false;

        }

        /// <summary>
        /// 读取指定轴原点信号
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetAxisHome(int nAxisNo)
        {
            //0 ALM 伺服报警 1 ON 0 OFF 0x1
            //1 EL+ 正硬限位            0x2
            //2 EL- 负硬限位            0x4
            //3 EMG 急停信号            0x8
            //4 ORG 原点信号            0x10    
            //6 SL+ 正软限位            0x40
            //7 SL- 负软限位            0x80


            uint sts = 0;//轴状态字
            sts = LTDMC.dmc_axis_io_status((ushort)m_nCardIndex, (ushort)nAxisNo);//读取指定轴有关信号的状态
            if ((sts & 0x10) != 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 读取指定轴负极限
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetNegLimit(int nAxisNo)
        {
            //0 ALM 伺服报警 1 ON 0 OFF 0x1
            //1 EL+ 正硬限位            0x2
            //2 EL- 负硬限位            0x4
            //3 EMG 急停信号            0x8
            //4 ORG 原点信号            0x10    
            //6 SL+ 正软限位            0x40
            //7 SL- 负软限位            0x80
            uint sts = 0;//轴状态字
            sts = LTDMC.dmc_axis_io_status((ushort)m_nCardIndex, (ushort)nAxisNo);//读取指定轴有关信号的状态
            if ((sts & 0x4) != 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 读取指定轴正极限
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override bool GetPosLimit(int nAxisNo)
        {
            //0 ALM 伺服报警 1 ON 0 OFF 0x1
            //1 EL+ 正硬限位            0x2
            //2 EL- 负硬限位            0x4
            //3 EMG 急停信号            0x8
            //4 ORG 原点信号            0x10    
            //6 SL+ 正软限位            0x40
            //7 SL- 负软限位            0x80


            uint sts = 0;//轴状态字
            sts = LTDMC.dmc_axis_io_status((ushort)m_nCardIndex, (ushort)nAxisNo);//读取指定轴有关信号的状态
            if ((sts & 0x2) != 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 指定轴减速停止
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void StopAxis(int nAxisNo)
        {

            short rtn = 0;
            rtn += LTDMC.dmc_stop((ushort)m_nCardIndex, (ushort)nAxisNo, 0);//0 减速停止 1紧急停止
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡减速停止失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡减速停止失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 指定轴紧急停止
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void EmgAxis(int nAxisNo)
        {

            short rtn = 0;
            rtn += LTDMC.dmc_stop((ushort)m_nCardIndex, (ushort)nAxisNo, 1);//0 减速停止 1紧急停止
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡紧急停止失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡紧急停止失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 指定轴位置清零
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <returns></returns>
        public override void SetPosZero(int nAxisNo)
        {

            short rtn = 0;
            rtn += LTDMC.dmc_set_position_unit((ushort)m_nCardIndex, (ushort)nAxisNo, 0);// 设置轴的指令脉冲计数器绝对位置为 0
            rtn += LTDMC.dmc_set_encoder_unit((ushort)m_nCardIndex, (ushort)nAxisNo, 0);//设置编码器计数初始值为 0
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡位置清零失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡位置清零失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }

        /// <summary>
        /// 读取输入信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadIn(int nCardN0, int nIndex)
        {
            short rtn = 0;
            rtn = LTDMC.dmc_read_inbit((ushort)m_nCardIndex, (ushort)nIndex);//读取使能状态 0 低电平有效接通 1高电平高电平无效 取值范围0 -15
            if (rtn != 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 读取输出信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        public override bool ReadOut(int nCardN0, int nIndex)
        {
            short rtn = 0;
            rtn = LTDMC.dmc_read_outbit((ushort)m_nCardIndex, (ushort)nIndex);//读取使能状态 0 低电平有效接通 1高电平高电平无效
            if (rtn != 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 输出信号
        /// </summary>
        /// <param name="nIndex"></param>
        /// <param name="bBit"></param>
        /// <returns></returns>
        public override void WriteOut(int nCardN0, int nIndex, bool bBit)
        {
            short rtn = 0;
            int on_off;
            if (bBit == true)
            {
                on_off = 0;
            }
            else
            {
                on_off = 1;
            }
            rtn = LTDMC.dmc_write_outbit((ushort)m_nCardIndex, (ushort)nIndex, (ushort)on_off);//读取使能状态 0 低电平 1高电平
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡输出信号失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nIndex, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡输出信号失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nIndex, rtn));
            }
        }

        /// <summary>
        /// 指定轴绝对位置移动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nPos"></param>
        /// <param name="nSpeed"></param>
        /// <returns></returns>
        public override void AbsMove(int nAxisNo, double Pos, double Speed)
        {

            short rtn = 0;
            rtn += LTDMC.dmc_stop((ushort)m_nCardIndex, (ushort)nAxisNo, 1);//0 减速停止 1紧急停止
            Thread.Sleep(5);
            double acc = SystemCfg.AxisCfg[nAxisNo].Acc;//加速时间
            double dec = SystemCfg.AxisCfg[nAxisNo].Dec ;//减速时间
            double start = SystemCfg.AxisCfg[nAxisNo].StartSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;//起始速度
            double nPos = Pos * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            double nSpeed = Speed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            //起始速度 最大速度 加速度 减速度 停止速度 
            rtn += LTDMC.dmc_set_profile_unit((ushort)m_nCardIndex, (ushort)nAxisNo, start, nSpeed, acc, dec, 0);
            // 卡号 轴号 目标位置  运动模式 0 相对运动 1 绝对运动
            rtn += LTDMC.dmc_pmove_unit((ushort)m_nCardIndex, (ushort)nAxisNo, nPos, 1);
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡绝对定位失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡绝对定位失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }
        }
 
        /// <summary>
        /// 指定轴相对运动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nPos"></param>
        /// <param name="nSpeed"></param>
        /// <returns></returns>
        public override void RelMove(int nAxisNo, double Pos, double Speed)
        {

            short rtn = 0;
            rtn += LTDMC.dmc_stop((ushort)m_nCardIndex, (ushort)nAxisNo, 1);//0 减速停止 1紧急停止
            Thread.Sleep(5);
            double acc = SystemCfg.AxisCfg[nAxisNo].Acc ;//加速时间
            double dec = SystemCfg.AxisCfg[nAxisNo].Dec ;//减速时间
            double start = SystemCfg.AxisCfg[nAxisNo].StartSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;//起始速度
            double nPos = Pos * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            double nSpeed = Speed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            //起始速度 最大速度 加速度 减速度 停止速度 
            rtn += LTDMC.dmc_set_profile_unit((ushort)m_nCardIndex, (ushort)nAxisNo, start, nSpeed, acc, dec, 0);
            // 卡号 轴号 目标位置  运动模式 0 相对运动 1 绝对运动
            rtn += LTDMC.dmc_pmove_unit((ushort)m_nCardIndex, (ushort)nAxisNo, nPos, 0);
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡相对定位失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡相对定位失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }

        }

        /// <summary>
        /// 指定轴Jog运动
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="nSpeed"></param>
        /// <param name="bPositive"></param>
        /// <returns></returns>
        public override void JogMove(int nAxisNo, double Speed, bool MovDir)
        {
            short rtn = 0;

            rtn += LTDMC.dmc_stop((ushort)m_nCardIndex, (ushort)nAxisNo, 1);//0 减速停止 1紧急停止
            Thread.Sleep(5);
            double acc = SystemCfg.AxisCfg[nAxisNo].Acc ;//加速时间
            double dec = SystemCfg.AxisCfg[nAxisNo].Dec;//减速时间
            double start = SystemCfg.AxisCfg[nAxisNo].StartSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;//起始速度
            double nSpeed = Speed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
            int on_off = 2;
            if (MovDir == true)
            {
                on_off = 1;
            }
            else
            {
                on_off = 0;
            }
            rtn += LTDMC.dmc_set_profile_unit((ushort)m_nCardIndex, (ushort)nAxisNo, start, nSpeed, acc, dec, 0);//设置单轴运算速度曲线
            rtn += LTDMC.dmc_vmove((ushort)m_nCardIndex, (ushort)nAxisNo, (ushort)on_off);//0 负方向 1正方向
            if (rtn != 0)
            {
                //MessageBox.Show(string.Format("雷赛控制卡Jog运动失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡Jog运动失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
            }

        }

        /// <summary>
        /// 指定轴回原点 
        /// </summary>
        /// <param name="nAxisNo"></param>
        /// <param name="MoveDir"></param>
        /// <returns></returns>
        public override void HomeMove(int nAxisNo, int MoveDir, uint HomeMode)
        {
            Task.Factory.StartNew(() =>
            {
                short rtn = 0;
                ushort postion = 0;
                //27 负方向找原点
                //28 负方向找原点  在原点上
                //23 正方向找原点
                //24 正方向找原点 在原点上
                if (MoveDir == 0)
                {
                    postion = 28;
                }
                if (MoveDir == 1)
                {
                    postion = 24;
                }
                double acc = SystemCfg.AxisCfg[nAxisNo].Acc;//加速时间
                double dec = SystemCfg.AxisCfg[nAxisNo].Dec;//减速时间
                double start = SystemCfg.AxisCfg[nAxisNo].StartSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;//起始速度
                double Hspeed = SystemCfg.AxisCfg[nAxisNo].OrgHSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
                double Lspeed = SystemCfg.AxisCfg[nAxisNo].OrgLSpeed * SystemCfg.AxisCfg[nAxisNo].PluseRatio;
                rtn += LTDMC.nmc_set_home_profile((ushort)m_nCardIndex, (ushort)nAxisNo, (ushort)postion, Lspeed, Hspeed, acc, dec, 0);
                rtn += LTDMC.nmc_home_move((ushort)m_nCardIndex, (ushort)nAxisNo);
                if (rtn != 0)
                {
                    //MessageBox.Show(string.Format("雷赛控制卡回零运动失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                    //LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制卡回零运动失败 卡号 轴号 原因{0}{1}{2}", m_nCardIndex, nAxisNo, rtn));
                }
            });
        }

        public override void InitExtIoCard(int CarsNO)
        {
            //short rtn = 0;
            //rtn = LTDMC.dmc_soft_reset((ushort)m_nCardIndex, (ushort)nIndex);//读取使能状态 0 低电平有效接通 1高电平高电平无效 取值范围0 -15
        }

        public override void DeInitExtIoCard(int Card)
        {
           
        }

        public override bool ReadExtIn(int CardNo, int nIndex)
        {
          return false;
        }

        public override bool ReadExtOut(int CardNo, int nIndex)
        {
            return false;
        }

        public override void WriteExtOut(int CardNo, int nIndex, bool bBit)
        {
        
        }
      
        public override bool GetBusState()
        {
            int rtn = 0;
            ushort code = 0;
            //读取 EtherCAT 总线状态
            //控制卡卡号  EtherCat总线端口号 固定为2 EtherCat 总线状态 0 表示正常
            rtn = LTDMC.nmc_get_errcode((ushort)m_nCardIndex, 2, ref code);
            if (rtn == 0)
            {
                if (code == 0)
                {
                    return true;
                }
            }
          //  MessageBox.Show(string.Format("雷赛控制总线报警 原因{0}", rtn));
           // LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制总线报警 卡号 轴号 原因{0}", m_nCardIndex, rtn));
            return false;
        }

        public override void ClearBusAlarm()
        {
            //清除 EtherCAT 总线错误码
            int rtn = 0;
            //CardNo 控制卡卡号  PortNum EtherCAT 总线端口号，固定为 2
            rtn = LTDMC.nmc_clear_errcode((ushort)m_nCardIndex, 2);
            if (rtn != 0)
            {
                MessageBox.Show(string.Format("雷赛控制清除总线报警失败 原因{0}",  rtn));
                LogView.GetInstance().ShowLog(LogView.LogType.Error, string.Format("雷赛控制清除总线报警失败 卡号 轴号 原因{0}", m_nCardIndex, rtn));
            }
        }

        public override bool HomeMove1(int nAxisNo, int MoveDir)
        {
            throw new NotImplementedException();
        }

        public override void OpenCmd(int cardNum, ushort channel, uint dir)
        {
            throw new NotImplementedException();
        }

        public override void CloseCmd(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void SetCmdData(int cardNum, ushort channel, double[] data)
        {
            throw new NotImplementedException();
        }

        public override int GetCmdFIFOCount(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void SetCmdDO(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void CloseCmdDO(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override void ResetCmd(int cardNum, ushort channel)
        {
            throw new NotImplementedException();
        }

        public override bool HomeOK(int nAxisNo)
        {
            throw new NotImplementedException();
        }
    }
}

